const express = require('express');
const router = express.Router();
const dbConfig = require('../db.js')
const mysql = require('mysql');
const sql = require('../sql.js')

const multiparty = require('multiparty');

var connection = mysql.createConnection(dbConfig)
connection.connect();

router.use('/queryAllNotice',(req,res,next)=>{
	let target = '0';
	connection.query(sql.notice.queryAllNotice,target,(err,result,fields)=>{
		if(err){
			console.log(err);
			res.send(500)
		}
		res.status(200).send(result)
	})
})
router.use('/queryUserNotice',(req,res,next)=>{
	let username = req.query.username;
	connection.query(sql.notice.queryAllNotice,username,(err,result,fields)=>{
		if(err){
			console.log(err);
			res.send(500)
		}
		res.status(200).send(result)
	})

})
router.use('/insertNotice',(req,res,next)=>{
	let notice = req.body;
	
	let n = [notice.title,notice.content,notice.time,notice.target,notice.m_name];
	console.log(n)
	connection.query(sql.notice.insertNotice,n,(err,result,fields)=>{
		if(err){
			console.log(err);
			res.send(500)
		}
		res.send('ok')
	})
})

router.use('/deleteNotice',(req,res,next)=>{
	let id = req.query.id;
	connection.query(sql.notice.deleteNotice,id,(err,result,fields)=>{
		if(err){
			console.log(err);
			res.send(500)
		}
		res.send('ok')
	})

})

module.exports = router;